package IBBB.model;

import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import server.Word;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class WordProperty {
    public String getWord() {
        return word.get();
    }

    public StringProperty wordProperty() {
        return word;
    }

    private StringProperty word;

    public String getPhone() {
        return phone.get();
    }

    public StringProperty phoneProperty() {
        return phone;
    }

    public String getTrans() {
        return trans.get();
    }

    public StringProperty transProperty() {
        return trans;
    }

    private StringProperty phone;
    private StringProperty trans;

    public StringBinding hiddenHtmlProperty() {
        return hiddenHtml;
    }

    private StringBinding hiddenHtml;

    public String getPresentHtml() {
        return presentHtml.get();
    }

    public StringBinding presentHtmlProperty() {
        return presentHtml;
    }

    private StringBinding presentHtml;

    private String modifyToHtml(String s) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));
        String line;
        StringBuilder buf = new StringBuilder();
        try {
            while((line = reader.readLine()) != null) {
                buf.append(line).append("<br>");
            }
        } catch (Exception e) {
            //TODO: handle error
            e.printStackTrace();
        }

        return buf.toString();
    }

    public WordProperty(Word word) {
        this.word = new SimpleStringProperty(word.getWord());
        this.phone = new SimpleStringProperty(modifyToHtml(word.getPhone()));
        this.trans = new SimpleStringProperty(modifyToHtml(word.getSynos()));
        hiddenHtml = Bindings.createStringBinding(()->{
            String h1 = "<html><body><br><br><div><h1 style=\"text-align:center\">";
            String h2 = "</h1><h2 style=\"font-size:20px;color:gray;text-align:center\">";
            String h3 = "</h2></div></body></html>";
            return h1 +
                    this.word.get() +
                    h2 +
                    this.phone.get() +
                    h3;
        }, this.word, this.phone);

        presentHtml = Bindings.createStringBinding(()->{
            String h1 = "<html><body><div><h1>";
            String h2 = "</h1><h2 style=\"font-size:20px;color:gray\">";
            String h3 = "</h2></div><p>";
            String h4 = "</p></body></html>";
            return h1 + this.word.get()
                    + h2 + this.phone.get()
                    + h3 + this.trans.get()
                    + h4;
        }, this.word, this.phone, this.trans);
    }

    public void setWord(Word word) {
        this.word.set(word.getWord());
        this.phone.set(modifyToHtml(word.getPhone()));
        this.trans.set(modifyToHtml(word.getSynos()));
    }
}
